﻿/**
 * 基础计算方法
 */
export default class NumberCalc {
    /**
     * 加法
     * @param arg1 加数
     * @param arg2 加数
     * @returns 结果
     */
    public static Add(arg1: number, arg2: number): number {
        var arg1Arr = arg1.toString().split("."),
            arg2Arr = arg2.toString().split("."),
            d1 = arg1Arr.length == 2 ? arg1Arr[1] : "",
            d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
        var maxLen = Math.max(d1.length, d2.length);
        var m = Math.pow(10, maxLen);
        var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
        var d = arguments[2];
        return typeof d === "number" ? Number((result).toFixed(d)) : result;
    }
    /**
     * 减法
     * @param {any} arg1
     * @param {any} arg2
     */
    public static Sun(arg1: number, arg2: number): number {
        return NumberCalc.Add(arg1, -arg2);
    }
    /**
     * 乘法
     * @param {any} arg1
     * @param {any} arg2
     */
    public static Mul(arg1: number, arg2: number): number {
        var r1 = arg1.toString(),
            r2 = arg2.toString(),
            m: number, resultVal: number, d = arguments[2];
        m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0);
        resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m);
        return resultVal;
    }
    /**
     * 除法
     * @param {any} arg1
     * @param {any} arg2
     */
    public static Div(arg1: number, arg2: number): number {
        var r1 = arg1.toString(),
            r2 = arg2.toString(),
            m: number, resultVal: number;
        m = (r2.split(".")[1] ? r2.split(".")[1].length : 0) - (r1.split(".")[1] ? r1.split(".")[1].length : 0);
        resultVal = Number(r1.replace(".", "")) / Number(r2.replace(".", "")) * Math.pow(10, m);
        return resultVal;
    }
}
